package com.dc.web.controller;

import com.dc.dao.CalibrationShipOldDao;
import com.dc.dao.vo.CalibrationShipOldReturnVO;
import com.dc.entity.CalibrationShipOldEntity;
import com.dc.entity.UserEntity;
import com.dc.model.AjaxResult;
import com.dc.model.PageListBody;
import com.dc.service.CalibrationService;
import com.dc.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 船体割旧
 */
@Api(tags = "1-船体割旧")
@RestController
@RequestMapping("/api/calibration_ship_old")
@Slf4j
public class CalibrationShipOldController {

    @Resource
    UserService userService;

    @Resource
    CalibrationShipOldDao calibrationShipOldDao;

    @Resource
    CalibrationService calibrationService;

    @GetMapping("/list")
    public AjaxResult list(PageListBody body) {

        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Page<CalibrationShipOldReturnVO> all = calibrationShipOldDao.findAllByPage(pageRequest);
        return AjaxResult.success(all);
    }

    @GetMapping("/{id}")
    public AjaxResult show(@PathVariable("id") Long id) {

        CalibrationShipOldEntity entity = null;

        Optional<CalibrationShipOldEntity> optional = calibrationShipOldDao.findById(id);
        if(optional.isPresent()) {
            entity = optional.get();
        }

        return AjaxResult.success(entity);
    }

    @PostMapping()
    public AjaxResult add(@RequestHeader HttpHeaders httpHeaders, @RequestBody CalibrationShipOldEntity entity) {

        UserEntity user = userService.getUser(httpHeaders);
        entity.setId(null);
        entity.setUpdater(user.getUserName());
        // 动态计算
        calibrationService.calc(entity);

        CalibrationShipOldEntity save = calibrationShipOldDao.save(entity);

        return AjaxResult.success(save);
    }

    @PutMapping("/{id}")
    public AjaxResult update(@RequestHeader HttpHeaders httpHeaders,
                             @PathVariable("id") Long id,
                             @RequestBody CalibrationShipOldEntity entity) {
        UserEntity user = userService.getUser(httpHeaders);
        entity.setId(id);
        entity.setUpdater(user.getUserName());
        // 动态计算
        calibrationService.calc(entity);

        CalibrationShipOldEntity save = calibrationShipOldDao.save(entity);

        return AjaxResult.success(save);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        calibrationShipOldDao.deleteById(id);

        return AjaxResult.success();
    }


}
